﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class 药品盘点 : Form
    {
        public 药品盘点()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)//刷新
        {
            fresh();

        }

        private void button2_Click(object sender, EventArgs e)//进入
        {
            Form f = new Form();
            f = new 药库盘点单录入((int)dataGridView1.CurrentRow.Cells["盘点单号"].Value);

            f.ShowDialog();
            fresh();

        }

        private void button3_Click(object sender, EventArgs e)//新单
        {
            int pddh = 0;
            try
            {

                string cmdtext1 = "select pddh from  yk_pdd0  order by pddh desc ";
                SqlConnection cn = new SqlConnection(SQL.conStr);
                {
                    cn.Open();
                    SqlCommand com = new SqlCommand(cmdtext1, cn);
                    pddh = (int)com.ExecuteScalar();
                }
            }
            catch { MessageBox.Show("首次盘点"); }
            Form f = new 药库盘点单录入(pddh + 1);
            f.ShowDialog();
            fresh();

        }

        private void button4_Click(object sender, EventArgs e)//删除
        {
            try
            {
                SqlConnection cn = new SqlConnection(SQL.conStr);
                cn.Open();
                string cmdtext3 = "   BEGIN TRANSACTION  " +
                                  "   Delete Yk_PDD0 where PDDH='" + dataGridView1.CurrentRow.Cells["盘点单号"].Value + "'"+
                                  "   Delete Yk_PDD1 where PDDH='" + dataGridView1.CurrentRow.Cells["盘点单号"].Value + "'" +
                                  "   Delete Yk_RKD0 where rkdh='" + dataGridView1.CurrentRow.Cells["盘盈单"].Value + "'" +
                                  "   Delete Yk_RKD1  where rkdh='" + dataGridView1.CurrentRow.Cells["盘盈单"].Value + "'" +
                                  "   Delete Yk_CKD0 where ckdh='" + dataGridView1.CurrentRow.Cells["盘亏单"].Value + "'" +
                                  "   Delete Yk_CKD1 where ckdh='" + dataGridView1.CurrentRow.Cells["盘亏单"].Value + "'" +
                                  "   COMMIT TRANSACTION ";
                SqlCommand cmd = new SqlCommand(cmdtext3, cn);
                cmd.ExecuteNonQuery();
                fresh();
            }
            catch  { return; } 
        }

        private void button8_Click(object sender, EventArgs e)//审核
        {
            try
            {
                SqlConnection cn = new SqlConnection(SQL.conStr);
                cn.Open();
                string cmdtext3 = "   BEGIN TRANSACTION  " +
                                  "UPDATE YK_PDD0 SET " +
                                  "SHRY = '" + LoginForm.username + "', " +
                                  "JZRY = '', " +
                                  "FLAG = 17  " +
                                  "WHERE PDDH = '" + dataGridView1.CurrentRow.Cells["盘点单号"].Value+"'";


                cmdtext3 = cmdtext3 + "   UPDATE YK_RKD0 SET " +
                                      "SHRY = '" + LoginForm.username + "', " +
                                      "JZRY = '', " +
                                      "FLAG = 17  " +
                                     "  WHERE RKDH = '" + dataGridView1.CurrentRow.Cells["盘盈单"].Value+"'";
                cmdtext3 = cmdtext3 + "   UPDATE YK_CKD0 SET " +
                                      "SHRY = '" + LoginForm.username + "', " +
                                      "JZRY = '', " +
                                      "FLAG = 17  " +
                                     "  WHERE CKDH = '" + dataGridView1.CurrentRow.Cells["盘亏单"].Value +"'"+
                                     "    COMMIT TRANSACTION ";


                SqlCommand cmd = new SqlCommand(cmdtext3, cn);
                cmd.ExecuteNonQuery();
                fresh();
            }
            catch  { return; } 
        }

        private void button5_Click(object sender, EventArgs e)//消审
        {
            try
            {
                SqlConnection cn = new SqlConnection(SQL.conStr);
                cn.Open();
                string cmdtext3 = "   BEGIN TRANSACTION  " +
                                  "UPDATE YK_PDD0 SET " +
                                  "SHRY = '', " +
                                  "JZRY = '', " +
                                  "FLAG = 16  " +
                                  "WHERE PDDH = '" + dataGridView1.CurrentRow.Cells["盘点单号"].Value+"'";

                cmdtext3 = cmdtext3 + "   UPDATE YK_RKD0 SET " +
                                  "SHRY = '', " +
                                  "JZRY = '', " +
                                  "FLAG = 16  " +
                                     "  WHERE RKDH = '" + dataGridView1.CurrentRow.Cells["盘盈单"].Value+"'";
                cmdtext3 = cmdtext3 + "   UPDATE YK_CKD0 SET " +
                                  "SHRY = '', " +
                                  "JZRY = '', " +
                                  "FLAG = 16  " +
                                     "  WHERE CKDH = '" + dataGridView1.CurrentRow.Cells["盘亏单"].Value +"'"+
                                     "    COMMIT TRANSACTION ";

                SqlCommand cmd = new SqlCommand(cmdtext3, cn);
                cmd.ExecuteNonQuery();
                fresh();
            }
            catch { return; } 
        }

        private void button6_Click(object sender, EventArgs e)//记账
        {

            int jzrq = 0;//记账日期
            SqlConnection cn1 = new SqlConnection(SQL.conStr);
            {
                cn1.Open();
                string cmdtext4 = "select  convert(int,(select Convert(DateTime,getdate())))+1";
                SqlCommand cmd = new SqlCommand(cmdtext4, cn1);
                jzrq = (int)cmd.ExecuteScalar();
            }
            SqlConnection cn2 = new SqlConnection(SQL.conStr);
            {
                cn2.Open();
                string cmdtext3 = "BEGIN TRANSACTION  " +
                                  "   UPDATE YK_PDD0 SET " +
                                  "JZRY = '" + LoginForm.username + "', " +
                                  "FLAG = 19,  " +
                                  "jzrq =" + jzrq +
                                  "  WHERE pddh = '" + dataGridView1.CurrentRow.Cells["盘点单号"].Value+"'";

                cmdtext3 = cmdtext3 + "   UPDATE YK_RKD0 SET " +
                                     "JZRY = '" + LoginForm.username + "', " +
                                     "FLAG = 19,  " +
                                     "jzrq =" + jzrq +
                                     "  WHERE RKDH = '" + dataGridView1.CurrentRow.Cells["盘盈单"].Value+"'";
                cmdtext3 = cmdtext3 + "   UPDATE YK_CKD0 SET " +
                                     "JZRY = '" + LoginForm.username + "', " +
                                     "FLAG = 19,  " +
                                     "jzrq =" + jzrq +
                                     "  WHERE CKDH = '" + dataGridView1.CurrentRow.Cells["盘亏单"].Value +"'"+
                                     "    COMMIT TRANSACTION ";


                string cmd_insert = "INSERT INTO YP_CRKJZ SELECT YPBM,-SL FROM YK_CKD1 where CKDH='" + dataGridView1.CurrentRow.Cells["盘亏单"].Value+"'";
                cmd_insert = cmd_insert + "  INSERT INTO YP_CRKJZ SELECT YPBM,SL FROM YK_RKD1 WHERE RKDH='" + dataGridView1.CurrentRow.Cells["盘盈单"].Value+"'";
                SqlCommand cmd1 = new SqlCommand(cmd_insert, cn2);
                cmd1.ExecuteNonQuery();
                SqlCommand cmd = new SqlCommand(cmdtext3, cn2);
                cmd.ExecuteNonQuery();

            }
            MessageBox.Show("记账成功");
            fresh();
        }


        private void button7_Click(object sender, EventArgs e)//关闭
        {
            this.Close();
        }

        private void 药品盘点_Load(object sender, EventArgs e)
        {
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.MultiSelect = false;
            fresh();     
        }

        public void fresh()//刷新
        {
            DataTable dt = new DataTable();
            string dtp1 = dateTimePicker1.Value.ToShortDateString();
            string dtp2 = dateTimePicker2.Value.ToShortDateString();
            SqlConnection cn = new SqlConnection(SQL.conStr);
            string addtext1;
            if (checkBox1.Checked)
            {
                if (checkBox2.Checked)
                {
                    if (checkBox3.Checked)
                        addtext1 = "  AND (p0.flag=16 OR p0.flag=17 OR p0.flag=19 ) ";
                    else addtext1 = " AND ( p0.flag=16 OR p0.flag=17 ) ";
                }
                else
                {
                    if (checkBox3.Checked)
                        addtext1 = "  AND (p0.flag=16 OR p0.flag=19)  ";
                    else addtext1 = "  AND p0.flag=16  ";
                }
            }
            else
            {
                if (checkBox2.Checked)
                {
                    if (checkBox3.Checked)
                        addtext1 = "  AND (p0.flag=17 OR p0.flag=19)  ";
                    else addtext1 = "  AND p0.flag=17  ";
                }
                else
                {
                    if (checkBox3.Checked)
                        addtext1 = "  AND p0.flag=19   ";
                    else addtext1 = "  ";
                }
            }
            
            
            try
            {
                cn.Open();
                string cmdtext1 = "select distinct pddh as 盘点单号,(select  convert(datetime,p0.rq-2)) as 盘点日期," +
                                "lrry as 录入,shry as 审核,jzry as 记账,(select  convert(datetime,jzrq-2)) as 记账日期,rkdh as 盘盈单,ckdh as 盘亏单,bz 备注, rkflag.mc as 状态 FROM YK_PDD0 p0   LEFT JOIN zd_rkflag rkflag ON p0.flag = rkflag.flag  " +
                                "WHERE p0.YKBM='401'   " +
                                addtext1 +
                                "AND p0.RQ >=(select  convert(int,(select Convert(DateTime,'" + dtp1 + "')))+2 )AND p0.RQ <=(select  convert(int,(select Convert(DateTime,'" + dtp2 + "')))+2 )  ORDER BY p0.PDDH";

                SqlDataAdapter da = new SqlDataAdapter(cmdtext1, cn);
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                dataGridView1.Columns[6].Width = 150;
                dataGridView1.Columns[7].Width = 150;
            }
            catch  { return; }
            if (dataGridView1.Rows.Count > 0)
                selectRow();
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)//双击，进入
        {
            Form f = new Form();

            f = new 药库盘点单录入((int)dataGridView1.CurrentRow.Cells["盘点单号"].Value);

            f.ShowDialog();
            fresh();
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            selectRow();
        }

        public void selectRow()//选中某行后，审核 记账 按钮的显示
        {
            if (dataGridView1.SelectedRows[0].Cells["状态"].Value.ToString().Trim() == "已记账")
            {
                button4.Enabled = false;
                button5.Enabled = false;
                button6.Enabled = false;
                button8.Enabled = false;
            }
            else if (dataGridView1.SelectedRows[0].Cells["状态"].Value.ToString().Trim() == "已审核")
            {
                button4.Enabled = false;
                button5.Enabled = true;
                button6.Enabled = true;
                button8.Enabled = false;
            }
            else if (dataGridView1.SelectedRows[0].Cells["状态"].Value.ToString().Trim() == "未审核")
            {
                button4.Enabled = true;
                button5.Enabled = false;
                button6.Enabled = false;
                button8.Enabled = true;
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            Form f = new Form();
            try
            {
                 f = new 药库入库单录入(System.Convert.ToInt32(dataGridView1.CurrentRow.Cells["盘盈单"].Value));
            }
            catch { MessageBox.Show("没有盘盈单"); return; }
            f.ShowDialog();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Form f = new Form();
            try
            {
                 f = new 药品出库单录入(System.Convert.ToInt32(dataGridView1.CurrentRow.Cells["盘亏单"].Value));
            }
            catch { MessageBox.Show("没有盘亏单"); return; }
            f.ShowDialog();
        }
    }
}